package com.zy.servlet.yhgl;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhang.sys.SysUser;
import com.zy.service.CommonService;
import com.zy.service.YhglService;

public class AddUser extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);

	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	@SuppressWarnings("unchecked")
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		response.setContentType("appliaction/x-json;charset=utf-8");
		PrintWriter out = response.getWriter();
		SysUser userBean = (SysUser) request.getSession().getAttribute(
				"userBean");
		if (userBean != null) {
			String userId = request.getParameter("userId");
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			String sex = request.getParameter("sex");
			String flag = request.getParameter("flag");
			String role = request.getParameter("role");
			String csid = request.getParameter("csid");
			String confirPassword = request.getParameter("confirPassword");

			SysUser user = new SysUser();
			user.setUserId(userId);
			user.setUserName(username);
			user.setPasswd(password);
			user.setSex(sex);
			user.setUserFlag(flag);
			user.setRoleId(role);
			user.setGroupId(csid);
			try {
				if (userId != null && !userId.equals("") && username != null
						&& !username.equals("") && password != null
						&& !password.equals("") && sex != null
						&& !sex.equals("") && flag != null && !flag.equals("")
						&& role != null && !role.equals("") && csid != null
						&& !csid.equals("") && confirPassword.equals(password)) {
					YhglService Service = new YhglService();
					Service.saveUser(user);
					out.println("{success:true,data:{rs:1}}");

				} else {
					out.println("{success:true,data:{rs:2}}");
				}
			} catch (RuntimeException e) {
				out.println("{success:true,data:{rs:2}}");
				e.printStackTrace();
			}
		}else{
			out.print(CommonService.getLoginTimeout());
		}
		out.flush();
		out.close();
	}

}
